<?php
//The object will be discard

class Upload_MIME { 
    $flag = 0;  
    $file_array = explode ( ".", $file ["name"] );  
    $file_extension = strtolower ( array_pop ( $file_array ) );  
      
    switch ($file_extension) {
        case "jpg" :  
        case "jpeg" :  
        case "jpe" :  
            $fh = fopen ( $file ["tmp_name"], "rb" );  
            $bin = fread ( $fh, 8 );  
            fclose ( $fh );  
            $strinfo = @unpack ( "C8chars", $bin );  
            $typecode = "";  
            foreach ( $strinfo as $num ) {  
                $typecode .= dechex ( $num );  
            }  
            if ($typecode == "ffd8") {  
                $flag = 1;  
            }  
            break;        
        case "xls" :  
            $fh = fopen ( $file ["tmp_name"], "rb" );  
            $bin = fread ( $fh, 8 );  
            fclose ( $fh );  
            $strinfo = @unpack ( "C8chars", $bin );  
            $typecode = "";  
            foreach ( $strinfo as $num ) {  
                $typecode .= dechex ( $num );  
            }  
            if ($typecode == "d0cf11e0a1b11ae1") {  
                $flag = 1;  
            }  
            break;  
        case "xlsx" :  
            // 2007 excel  
            $fh = fopen ( $file ["tmp_name"], "rb" );  
            $bin = fread ( $fh, 4 );  
            fclose ( $fh );  
            $strinfo = @unpack ( "C4chars", $bin );  
            $typecode = "";  
            foreach ( $strinfo as $num ) {  
                $typecode .= dechex ( $num );  
            }  
            echo $typecode;  
            if ($typecode == "504b34") {  
                $flag = 1;  
            }  
            break;  
    }  
    return $flag;  
}